

<?php

class PlaceController extends Zend_Controller_Action {

    private static $METHOD = "method";
    private static $UPDATE_TYPE = "updateType";
    private static $UPDATE_PLACE = "updatePlace";
    private static $UPDATE_ADDRESS = "updateAddress";
    private static $UPDATE_MENU = "updateMenu";
    private static $UPDATE_REVIEW = "updateReview";
    private static $UPDATE_MAP = "updateMap";
    private static $TYPE = "type";
    private static $ID = "id";

    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array()) {
        parent::__construct($request, $response, $invokeArgs);
    }

    public function preDispatch() {
//        $auth = Zend_Auth::getInstance();
//        if (!$auth->hasIdentity()) {
//            if ($this->_request->getActionName() != 'admin') {
//                $this->_redirect('admin');
//            }
//        }
    }

    public function addAction() {
        $this->buildHead();
        //get Type for places
        $db = new Model_Type();
        $result = $db->getAllType();
        $this->view->allType = $result;
        //address
        $db = new Model_Site();
        $result = $db->getAllSite();
        $this->view->allSite = $result;
    }

    public function indexAction() {
        $this->buildHead();
        $place = new Model_Place();
        $dataPlace = $place->getAllPlace();
        $address = new Model_Address();

        for ($index = 0; $index < count($dataPlace); $index++) {
            $pl = $dataPlace[$index];
            $addId = $pl['addressId'];
            $addressPl = $address->getAddressById($addId);
            $pl['address'] = $addressPl[0]['info'];
            $dataPlace[$index] = $pl;
        }

        $paginator = Zend_Paginator::factory($dataPlace);
        $paginator->setItemCountPerPage(6);
        $paginator->setPageRange(6);
        $currentPage = $this->_request->getParam('page', 1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->data = $paginator;
    }

    public function updateAction() {
        $this->buildHead();
        $id = $this->_request->getParam("id");
        $model = new Model_Place();
        $rs = $model->getPlaceDetailInfoById($id);
        //render new form        
        $this->view->place = $rs['place'];
        $this->view->address = $rs['address'];
        $this->view->menu = $rs['menu'];
        $this->view->review = $rs['review'];
        $this->view->map = $rs['map'];
        $this->view->type = $rs['type']['id'];
        $this->view->allSite = $rs['allSite'];
        $this->view->allType = $rs['alltype'];
    }

    public function doupdateAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $method = $this->_request->getParam(self::$METHOD);

        switch ($method) {

            case self::$UPDATE_TYPE : {
                    $id = $this->_request->getParam(self::$ID);
                    $type = $this->_request->getParam(self::$TYPE);
                    $model = new Model_Place();
                    $rs = $model->updateTypeOfPlace($id, $type);
                    if ($rs)
                        echo $this->returnDone();
                    else
                        echo $this->returnFail();
                    break;
                }
            case self::$UPDATE_PLACE: {
                    $id = $this->_request->getParam(self::$ID);
                    $name = $this->_request->getParam("placeName");
                    $phonenumber = $this->_request->getParam("placePhone");
                    $model = new Model_Place();
                    $rs = $model->updatePhoneAndNamePlace($id, $name, $phonenumber);
                    if ($rs)
                        echo $this->returnDone();
                    else
                        echo $this->returnFail();
                    break;
                }
            case self::$UPDATE_ADDRESS: {
                    $arr = $this->_request->getParams();
                    $model = new Model_Address();
                    $rs = $model->updateAddressById($arr);
                    if ($rs)
                        echo $this->returnDone();
                    else
                        echo $this->returnFail();
                    break;
                }
            case self::$UPDATE_MENU: {
                    $id = $this->_request->getParam(self::$ID);
                    $menuDetail = $this->_request->getParam("menuDetail");
                    $model = new Model_Menu();
                    $rs = $model->updateMenuById($id, $menuDetail);
                    if ($rs)
                        echo $this->returnDone();
                    else
                        echo $this->returnFail();
                    break;
                }
            case self::$UPDATE_REVIEW : {
                    $id = $this->_request->getParam(self::$ID);
                    $reviewContent = $this->_request->getParam("reviewContent");
                    $imageLinks = $this->_request->getParam("imageLinks");
                    $model = new Model_Review();
                    $rs = $model->updateReviewById($id, $reviewContent, $imageLinks);
                    if ($rs)
                        echo $this->returnDone();
                    else
                        echo $this->returnFail();
                    break;
                }
            case self::$UPDATE_MAP : {
                    $id = $this->_request->getParam(self::$ID);
                    $mapLink = $this->_request->getParam("mapLink");
                    $model = new Model_Map();
                    $rs = $model->updateMapById($id, $mapLink);
                    if ($rs)
                        echo $this->returnDone();
                    else
                        echo $this->returnFail();
                    break;
                }
        }
    }

    public function updatepriorityAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $priority = $this->_request->getParam("priority");
        $id = $this->_request->getParam("id");

        if ($priority != null && $id != null) {


            $model = new Model_Place();
            $rs = $model->updatePriority($id, $priority);
            if ($rs)
                echo $this->returnDone();
            else
                echo $this->returnFail();
        }
    }

    public function updateavatarAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $avatar = $this->_request->getParam("avatar");
        $id = $this->_request->getParam("id");

        if ($avatar != null && $id != null) {
            $model = new Model_Place();
            $rs = $model->updateAvatar($id, $avatar);
            if ($rs)
                echo $this->returnDone();
            else
                echo $this->returnFail();
        }
    }

    public function doinsertAction() {        
        $this->_helper->viewRenderer->setNoRender(true);
        $params = $this->_request->getParams();
        
        $address = null;
        $avatarLink = null;
        $menu = null;
        $review = null;
        $imageList = null;
        $mapLink = null;
        $type = null;
        $placeName = null;
        $placePhoneNumber = null;
        
        if (isset($params['avatarLink'])) {
            $avatarLink = $params['avatarLink'];
        }
        if (isset($params['type'])) {
            $type = $params['type'];
        }
        if (isset($params['placePhone'])) {
            $placePhoneNumber = $params['placePhone'];
        } else {
            $placePhoneNumber = "-1";
        }
        if (isset($params['placeName'])) {
            $placeName = $params['placeName'];
        }
        $params['mapLink'] = "empty";
        if (isset($params['addressNumber'])
                && isset($params['site'])
                && isset($params['addressStreet'])
                && isset($params['addressLon'])
                && isset($params['addressLat'])) {
            
            if (isset($params['addressWard']))
                $address['addressWard'] = $params['addressWard'];
            else
                $address['addressWard'] = "";
            
            $address['addressNumber'] = $params['addressNumber'];
            $address['site'] = $params['site'];
            $address['addressStreet'] = $params['addressStreet'];
            $address['addressLon'] = $params['addressLon'];
            $address['addressLat'] = $params['addressLat'];
            
            $address['info'] = $params['info'];            
            if (isset($params['addressDescription']))
                $address['addressDescription'] = $params['addressDescription'];
            else
                $address['addressDescription'] = "";
            
        }        
       
        if (isset($params['menuDetail'])) {
            $menu['menuDetail'] = "not ready";
        } else {
            $menu['menuDetail'] = "not ready";
        }        
         
        if (isset($params['reviewContent'])) {
            $review['reviewContent'] = $params['reviewContent'];
        } else {
             $review['reviewContent'] = "";
        }
        if (isset($params['imageLinks'])) {
            $imageList['imageLinks'] = $params['imageLinks'];
        } else {
             $imageList['imageLinks'] = "";
        }
        if (isset($params['mapLink'])) {
            $mapLink['mapLink'] = $params['mapLink'];
        } else {
            $mapLink['mapLink'] = "";
        }        
                
        if ($address != null &&
                $menu != null &&
                $review != null &&
                $imageList != null &&
                $mapLink != null &&
                $type != null &&
                $placeName != null) {
 
            //insert each item into database
            //Review
 echo 'inser2323t';
            $models = new Model_Review();
            $reviewId = $models->insertReview($review, $imageList['imageLinks']);
echo 'insert Model_Review';            
            $models = new Model_Address();
            $addressId = $models->insertAddress($address);
 echo 'insert Model_Address';             
            $models = new Model_Menu();
            $menuId = $models->insertMenu($menu);
            
            $models = new Model_Map();
            $mapId = $models->insertMap($mapLink);

            $models = new Model_Place();
            $placeId = $models->insertPlace($placeName, $addressId, $avatarLink, $menuId, $placePhoneNumber, $reviewId, $mapId, $type);

            if ($placeId != null) {
                $urlOptions = array('controller' => 'place', 'action' => 'add');
                $this->_helper->redirector->gotoRoute($urlOptions);
            } else {
                echo $this->returnFail();
            }
        }
        
    }

    public function deleteAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $id = $this->_request->getParam("id");
        $model = new Model_Place();
        $rs = $model->deletePlace($id);
        if ($rs)
            echo $this->returnDone();
        else
            echo $this->returnFail();
    }

    private function returnDone() {
        $array = array(
            "errorCode" => 0,
            "errorMessage" => "Query completed.",
        );
        return json_encode($array);
    }

    private function returnFail() {
        $array = array(
            "errorCode" => -1,
            "errorMessage" => "Query fail.",
        );
        return json_encode($array);
    }

    private function buildHead() {
        $base = $this->_request->getBaseUrl();
        $this->view->base = $base;
        $this->view->deleteAction = $base . '/place/delete';
        $this->view->doupdate = $base . "/place/doupdate";
        $this->view->doupdatepriority = $base . "/place/updatepriority";
        $this->view->doupdateavatar = $base . "/place/updateavatar";
        $this->view->baseUpdate = $base . '/place/update';
        $this->view->baseAdd = $base . "/place/add";
        $this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
        $this->view->headScript()->appendFile($base . "/js/Ajax.js", 'text/javascript');
        $this->view->headScript()->appendFile($base . "/js/sunnyAjax.js", 'text/javascript');
    }

}
?>

